// 山东pgis地图比例尺
let pgisScalse = [
  5.916587109091312e8,
  2.958293554545656e8,
  1.479146777272828e8,
  7.39573388636414e7,
  3.69786694318207e7,
  1.848933471591035e7,
  9244667.357955175,
  4622333.678977587,
  2311166.8394887936,
  1155583.4197443968,
  577791.7098721984,
  288895.8549360992,
  144447.9274680496,
  72223.9637340248,
  36111.9818670124,
  18055.9909335062,
  9027.9954667531,
  4513.99773337655,
  2256.998866688275,
];

// 山东天地图比例尺
let sdTianMapScalse = [
  295829355.45,
  147914677.73,
  73957338.86,
  36978669.43,
  18489334.72,
  9244667,
  4622334,
  2311166.84,
  1155583.42,
  577791.71,
  288895.85,
  144447.93,
  72223.96,
  36111.98,
  18055.99,
  9028,
  4514,
  2257,
];
let matrixIds = []; //矩阵标识列表，与地图级数保持一致

// 山东天地图分辨率
let tianMapresolutions = sdTianMapScalse.map(scalse => {
  let resolution = scalse * ((0.0254 / 96 / Math.PI / 2 / 6378137) * 360);
  return resolution;
});
tianMapresolutions.forEach((e, i) => {
  matrixIds.push(i + 1);
});

// scalse
// var startResolution = ol.extent.getWidth( ol.proj.get("EPSG:4326").getExtent()) / 256;
// var arr1 = new Array(19);
// for(var i = 0,ii = arr1.length;i < ii; ++i) {
//     arr1[i] = startResolution / Math.pow(2,i);
//     matrixIds[i] = i;
// }

// pgis 配置(山东)
export const shandongPgisConfig = {
  defaultJd: '118.6', //默认经度
  defaultWd: '36.4', //默认纬度
  defaultZoom: 8, //地图默认显示层级
  minZoom: 8, //最层级
  maxZoom: 18, //最小层级
  projection: 'EPSG:4326',
  tileGrid: {
    origin: [-180, 90],
    resolutions: tianMapresolutions,
    matrixIds: matrixIds,
  },
  tileUrlFunction: function (tileCoord) {
    // 获取给定缩放级别的原点
    var origin = this.getTileGrid().getOrigin(tileCoord[0]);
    // 获取给定缩放级别的分辨率
    var res = this.getTileGrid().getResolution(tileCoord[0]);
    // 瓦片大小
    var tileSize = [256, 256];
    var minX = origin[0] + tileCoord[1] * tileSize[0] * res;
    var minY = origin[1] + tileCoord[2] * tileSize[1] * res;
    var maxX = minX + tileSize[0] * res;
    var maxY = minY + tileSize[1] * res;
    var bound = [minX, minY, maxX, maxY];

    var originTileX = 0 + (res * 256) / 2;
    var originTileY = 0 + (res * 256) / 2;

    var centerX = (bound[0] + bound[2]) / 2;
    var centerY = (bound[1] + bound[3]) / 2;

    var x = Math.round(Math.abs((centerX - originTileX) / (res * 256)));
    var y = Math.round(Math.abs((centerY - originTileY) / (res * 256)));
    var z = tileCoord[0];
    return (
      'http://10.48.105.45:8080/EzServer/Maps/sl2018/EzMap?Service=getImage&Type=RGB&Col=' +
      y +
      '&Row=' +
      x +
      '&Zoom=' +
      z +
      '&ZoomOffset=0&V=1.1.0'
    );
    // return 'http://10.48.142.163/tileService/SDMap2000?SERVICE=WMTS&REQUEST=GetTile&version=1.0.0&layer=0&style=default&format=image/png&TileMatrixSet=SDMap2000&TileMatrix=' + z + '&TileRow=' + tileCoord[2] + '&TileCol=' + tileCoord[1];
  },
  tianMapWMTS: 'http://10.48.142.163/tileservice/sdmap2000?request=getcapabilities&service=wmts', //山东天地图WMTS服务xml文档
  pgisMapWMTS: 'http://10.48.105.45:8080/EzServer/Maps/sl2018?SERVICE=WMTS&REQUEST=GetCapabilities', //山东PGIS WMTS服务xml文档
};
